/*
 * 文件名： ChoosePlaceActivity.java
 * 描    述：联系人信息--联动修改省市区
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */
package com.miguo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.miguo.activity.base.BaseActivity;
import com.miguo.bean.AddressBean;
import com.miguo.component.BaseHeadView;
import com.miguo.db.AddressDB;
import com.miguo.framework.utils.Toast;
import com.miguo.ui.R;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ChoosePlaceActivity extends BaseActivity {

    private long parentid = 1;
    private long marking = 1;
    private String province;
    private String city;
    private String town;

    private ListView list;

    private AddressAdapter adapter;
    private List<AddressBean> array;
    private LinkedList<AddressBean> linkedList;
    protected BaseHeadView mBaseHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_place);
        findViewById();
        initView();
        setPlaceById();
    }

    private void findViewById() {
        mBaseHead = (BaseHeadView) findViewById(R.id.base_head);
        mBaseHead.setActivity(this);
        mBaseHead.setTitle("请选择");
        list = (ListView) findViewById(R.id.listview);
    }

    private void initView() {
        array = new ArrayList<AddressBean>();
        linkedList = new LinkedList<AddressBean>();

        adapter = new AddressAdapter(linkedList);
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new ItemClickListener());
    }

    private void setPlaceById() {
        array = AddressDB.getInstance(mContext).Parentid(parentid);
        if (array != null && array.size() > 0) {
            if (parentid == 1) {
                adapter.addList(array);
            } else {
                linkedList.clear();
                adapter.addList(array);
            }
            adapter.notifyDataSetChanged();
            array.clear();
        } else {
            if (parentid == 1) {
                Toast.show(mContext, "编辑地址失败~请稍候重试");
            } else {
                Intent intent = new Intent();
                intent.putExtra("province", province);
                intent.putExtra("city", city);
                intent.putExtra("town", town);
                setResult(RESULT_OK, intent);
                ChoosePlaceActivity.this.finish();
            }
        }
    }

    class ItemClickListener implements OnItemClickListener {
        public void onItemClick(AdapterView<?> click, View view, int position,
                                long row) {
            if (marking == 1) {
                province = linkedList.get(position).getName();
            } else if (marking == 2) {
                city = linkedList.get(position).getName();
            } else if (marking == 3) {
                town = linkedList.get(position).getName();
            }
            marking++;
            parentid = linkedList.get(position).getId();
            setPlaceById();
        }
    }

    class AddressAdapter extends BaseAdapter {
        private LinkedList<AddressBean> mAddressList;

        public AddressAdapter(LinkedList<AddressBean> addressList) {
            this.mAddressList = addressList;
        }

        @Override
        public int getCount() {
            return mAddressList.size();
        }

        @Override
        public Object getItem(int position) {
            return mAddressList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                LayoutInflater layoutInflator = LayoutInflater.from(parent
                        .getContext());
                convertView = layoutInflator.inflate(R.layout.list_item_place,
                        parent, false);
                holder = new ViewHolder();
                holder.address = ((TextView) convertView
                        .findViewById(R.id.address));
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.address.setText(mAddressList.get(position).getName());
            return convertView;
        }

        public void addList(List<AddressBean> data) {
            mAddressList.addAll(data);
        }

    }

    static class ViewHolder {
        private TextView address;
    }

}
